package com.jaydenxiao.common.commonwidget;

/**
 * Created by hasee on 2017/10/15.
 */

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

import com.jaydenxiao.common.R;


public class ColorTextView extends View {
    private String mTitleText;
    private int mTitleTextColor;
    private int mTitleTextSize;
    private int ctvBackgroundColor;
    private int mCornerSize;
    private Rect mtitleBound;
    private Paint mtitlePaint;

    public ColorTextView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ColorTextView(Context context) {
        this(context, null);
    }

    /**
     * @param context
     * @param attrs
     * @param defStyle
     */
    public ColorTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        TypedArray a = context.getTheme()
                .obtainStyledAttributes(attrs,
                        R.styleable.ColorTextView, defStyle, 0);
        int n = a.getIndexCount();

        for (int i = 0; i < n; i++) {
            int attr = a.getIndex(i);

            if (attr == R.styleable.ColorTextView_ctvText) {
                mTitleText = a.getString(attr);

            } else if (attr == R.styleable.ColorTextView_ctvTextColor) {
                mTitleTextColor = a.getColor(attr, Color.BLACK);

            } else if (attr == R.styleable.ColorTextView_ctvTextSize) {
                mTitleTextSize = a.getDimensionPixelSize(attr,
                        (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 16,
                                getResources().getDisplayMetrics()));


            } else if (attr == R.styleable.ColorTextView_ctvBackground) {
                ctvBackgroundColor = a.getColor(attr, Color.WHITE);

            } else if (attr == R.styleable.ColorTextView_ctvCornerSize) {
                mCornerSize = a.getInteger(attr, 0);
            }
        }

        a.recycle();
        mtitlePaint = new Paint();
        mtitlePaint.setTextSize(mTitleTextSize);
        mtitleBound = new Rect();
        mtitlePaint.getTextBounds(mTitleText, 0, mTitleText.length(),
                mtitleBound);
    }

    public void setCtvBackgroundColor(int ctvBackgroundColor) {
        this.ctvBackgroundColor = ctvBackgroundColor;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int width;
        int height;

        if (widthMode == MeasureSpec.EXACTLY) {
            width = widthSize;
        } else {
            mtitlePaint.setTextSize(mTitleTextSize);
            mtitlePaint.getTextBounds(mTitleText, 0, mTitleText.length(),
                    mtitleBound);

            int desired = getPaddingLeft() + mtitleBound.width() +
                    getPaddingRight();
            width = (desired <= widthSize) ? desired : widthSize;
        }

        if (heightMode == MeasureSpec.EXACTLY) {
            height = heightSize;
        } else {
            mtitlePaint.setTextSize(mTitleTextSize);
            mtitlePaint.getTextBounds(mTitleText, 0, mTitleText.length(),
                    mtitleBound);

            int desired = getPaddingTop() + mtitleBound.height() +
                    getPaddingBottom();
            height = (desired <= heightSize) ? desired : heightSize;
        }

        setMeasuredDimension(width, height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG);
        paint.setAntiAlias(true);
        paint.setColor(ctvBackgroundColor);

        RectF rec = new RectF(0, 0, getMeasuredWidth(), getMeasuredHeight());
        canvas.drawRoundRect(rec, mCornerSize, mCornerSize, paint);

        mtitlePaint.setColor(mTitleTextColor);

        Paint.FontMetricsInt fontMetrics = mtitlePaint.getFontMetricsInt();
        int baseline = ((getMeasuredHeight() - fontMetrics.bottom +
                fontMetrics.top) / 2) - fontMetrics.top;
        canvas.drawText(mTitleText, getPaddingLeft(), baseline, mtitlePaint);
    }
}
